package com.qianqian.service.controller;

import com.qianqian.service.config.Logined;
import com.qianqian.service.model.CapitalAccount;
import com.qianqian.service.model.LoginUser;
import com.qianqian.service.model.Response;
import com.qianqian.service.service.CapitalAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/account")
public class CapitalAccountController {

    private static final double MIN_APPLY_YUE = 50;


    @Autowired
    CapitalAccountService capitalAccountService;

    @PostMapping(value = "/getAccount", produces = {"application/json;charset=UTF-8"})
    public Response getResult(@Logined LoginUser loginUser){
        CapitalAccount capitalAccount = capitalAccountService.selectByUserId(loginUser.getUserId());
        Response response = Response.success();
        response.add("account",capitalAccount);
        return response;
    }

    @PostMapping(value = "/applyYue", produces = {"application/json;charset=UTF-8"})
    public Response applyYue(@Logined LoginUser loginUser){
        Response response = Response.success();
        CapitalAccount capitalAccount = capitalAccountService.selectByUserId(loginUser.getUserId());
        if(capitalAccount.getYue()<this.MIN_APPLY_YUE){
            if(capitalAccount.getEdu()>capitalAccount.getDanCiEdu()){
                capitalAccount.setYue(capitalAccount.getYue()+capitalAccount.getDanCiEdu());
                capitalAccount.setEdu(capitalAccount.getEdu()-capitalAccount.getDanCiEdu());
                capitalAccount.setYingliYue(capitalAccount.getYingliYue()-capitalAccount.getDanCiEdu());
                capitalAccountService.updateAccount(capitalAccount);
                response.add("account",capitalAccount);
                return response;
            }else{
                return Response.fail("额度不足");
            }
        }
        return Response.fail("余额大于"+this.MIN_APPLY_YUE);
    }

    @PostMapping(value = "/bindingZhifubao", produces = {"application/json;charset=UTF-8"})
    public Response applyYue(@Logined LoginUser loginUser,String zhifubao){
        CapitalAccount capitalAccount = new CapitalAccount();
        capitalAccount.setUserId(loginUser.getUserId());
        capitalAccount.setZhifubao(zhifubao);
        capitalAccountService.updateAccount(capitalAccount);
        return Response.success("绑定成功");
    }

    @PostMapping(value = "/tiXianJifen", produces = {"application/json;charset=UTF-8"})
    public Response applyYue(@Logined LoginUser loginUser,Double amount){
        capitalAccountService.tiXianJifen(loginUser.getUserId(),amount);
        return Response.success("提现申请成功，24小时内到账至您的支付宝账户");
    }
}
